package microkernel

import (
	"context"
	"errors"
	"fmt"
	"time"
)

type DemoCollector struct {
	evtReceiver EventReceiver
	// agtCtx      context.Context
	stopChan chan struct{}
	name     string
	content  string
}

// 创建Collect 收集器
func NewCollect(name string, content string) *DemoCollector {
	return &DemoCollector{
		stopChan: make(chan struct{}),
		name:     name,
		content:  content,
	}
}

func (c *DemoCollector) Init(evtReceiver EventReceiver) error {
	fmt.Println("initialize collector", c.name)
	c.evtReceiver = evtReceiver
	return nil
}

func (c *DemoCollector) Start(agtCtx context.Context) error {
	fmt.Println("start collector", c.name)
	for {
		select {
		// 监听取消信号，收到立刻退出
		case <-agtCtx.Done():
			c.stopChan <- struct{}{}
			return nil
		default:
			time.Sleep(time.Millisecond * 50)
			// 这里应该运行收集器的处理逻辑
			// do something
			// 将处理结果给到agent
			c.evtReceiver.OnEvent(Event{c.name, c.content})
		}
	}
}

func (c *DemoCollector) Stop() error {
	fmt.Println("stop collector", c.name)
	select {
	case <-c.stopChan:
		return nil
	case <-time.After(time.Second * 1):
		return errors.New("failed to stop for timeout")
	}
}

func (c *DemoCollector) Destroy() error {
	fmt.Println(c.name, "released resources.")
	return nil
}
